<template>
  <el-dialog
    :title="title"
    width="40%"
    :visible.sync="isShow"
    @close="closeHandler"
  >
    <el-form
      :model="formData"
      ref="form"
      :rules="formRules"
      label-width="120px"
      size="small"
      v-if="isShow"
      
    >
      <el-form-item label="广告位" prop="advimgpos">
        <el-select
          v-model="formData.advimgpos"
          clearable
          placeholder="请选择广告位"
        >
          <el-option
            v-for="item in allPosition"
            :key="item.id"
            :label="item.advposname"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="广告图描述" prop="advimgdesc">
        <el-input
          v-model="formData.advimgdesc"
          type="textarea"
          placeholder="请选择广告图描述"
        ></el-input>
      </el-form-item>

      <el-form-item label="广告图链接" prop="advimglink">
        <el-input
          v-model="formData.advimglink"
          placeholder="请选择广告图链接"
        ></el-input>
      </el-form-item>

      <el-form-item label="广告图" prop="advimgsrc">
        <el-button v-model="formData.advimgsrc" type="primary"
          >点击上传</el-button
        >
      </el-form-item>

      <el-form-item label-position="right">
        <el-button size="small" @click="isShow = false">取消</el-button>
        <el-button size="small" type="primary" @click="OnSubmit"
          >确定</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addAdvImgApi } from "@/api/advpos";
import { getAdvImgInfoApi, editAdvImgApi } from "@/api/advImg";
export default {
  data() {
    return {
      isShow: false,
      formData: {
        advimgdesc: "",
        advimglink: "",
        advimgpos: "",
        advimgsrc: "",
      },
      formRules: {
        advimgdesc: [{ required: true, message: "请输入", trigger: "blur" }],
        advimglink: [{ required: true, message: "请输入", trigger: "blur" }],
        advimgpos: [{ required: true, message: "请输入", trigger: "change" }],
      },
    };
  },
  props: {
    allPosition: {
      type: Array,
      default: () => [],
    },
  },
  computed: {
    title() {
      return this.formData.id ? "修改广告图" : "添加广告图";
    },
  },
  methods: {
    async OnSubmit() {
      try {
        // 1. 表单校验
        await this.$refs.form.validate();
        // 2. 整理数据
        // 3. 发起网络请求
        this.formData.id
          ? await editAdvImgApi(this.formData)
          : await addAdvImgApi(this.formData);
        // 4. 提醒添加成功
        this.$message.success(this.formData.id ? "修改成功" : "添加成功");
        // 5. 关闭弹窗(重置表单，移除校验)
        this.isShow = false;
        // 6. 更新列表
        this.$emit("updateList");
      } catch (error) {
        console.log(error);
      }
    },
    // 关闭重置
    closeHandler() {
      this.formData = {
        advimgdesc: "",
        advimglink: "",
        advimgpos: "",
        advimgsrc: "",
      };
      this.$refs.form.clearValidate();
    },
    // 回显方式
    async replyViewData(rowId) {
      let res = await getAdvImgInfoApi(rowId);

      res.advimgpos *= 1; // 返回得是字符串、类型，下拉得每项value是数字类型

      this.formData = res;

      this.isShow = true;
    },
  },
};
</script>

<style lang="scss" scoped></style>
